/************************************************************************************/
/*  Program name CAPFAIMP_ASSET_KEYWORDS.sql                                        	*/
/*  Creater:  CHAN BILLY (PCCW - UniHub)                                         	*/
/*************   History   *************************                                */
/* 29 MAY, 2006   CHAN BILLY         First created                               	*/
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;
ALTER TRIGGER FA_ASSET_KEYWORDS_CT1 DISABLE;


DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFAIMP_ASSET_KEYWORDS';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_FA_ASSET_KEYWORDS
	FOR UPDATE;
	
	lv_Seq_ID NUMBER;
	

BEGIN
	FOR C1 IN Cur_F LOOP
		BEGIN		
			
			SELECT FA_ASSET_KEYWORDS_S.NEXTVAL
			INTO lv_Seq_ID
			FROM DUAL;
			
			UPDATE CRST_FA_ASSET_KEYWORDS
			SET CODE_COMBINATION_ID_N = lv_Seq_ID
			WHERE CODE_COMBINATION_ID = C1.CODE_COMBINATION_ID;
			
			UPDATE FA_ASSET_KEYWORDS
			SET SEGMENT1 = CAPBRIMP_MAPPING.RenameStr(Substr(SEGMENT1,1,4),SEGMENT1)
			WHERE CODE_COMBINATION_ID = C1.CODE_COMBINATION_ID;
			
			INSERT INTO FA_ASSET_KEYWORDS
			VALUES
			(
			lv_Seq_ID			,
			C1.SEGMENT1,
			C1.SEGMENT2            ,
			C1.SEGMENT3            ,
			C1.SEGMENT4            ,
			C1.SEGMENT5            ,
			C1.SEGMENT6            ,
			C1.SEGMENT7            ,
			C1.SEGMENT8            ,
			C1.SEGMENT9            ,
			C1.SEGMENT10           ,
			C1.SUMMARY_FLAG        ,
			C1.ENABLED_FLAG        ,
			C1.START_DATE_ACTIVE   ,
			C1.END_DATE_ACTIVE     ,
			C1.LAST_UPDATE_DATE    ,
			C1.LAST_UPDATED_BY     ,
			C1.LAST_UPDATE_LOGIN   
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;
	
	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/
ALTER TRIGGER FA_ASSET_KEYWORDS_CT1 ENABLE;
